package com.lianniu.mgr.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.dao.IChannelDao;
import com.lianniu.mgr.cms.model.ChannelVo;
import com.lianniu.mgr.cms.service.IChannelService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ChannelServiceImpl implements IChannelService {

	private final static Logger logger = Logger.getLogger(ChannelServiceImpl.class);
	  
	@Resource
	IChannelDao channelDao;
	
	@Override
	public BaseResult insertChannel(ChannelVo curChannelVo) {
		// TODO Auto-generated method stub
	    logger.info("insertChannel begin");
	    logger.info("insertChannel: input data check begin");
	    logger.info("insertChannel curChannelVo:" +JSON.toJSONString(curChannelVo));	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curChannelVo.getChannelName())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }	    
	    if (StringUtil.isEmpty(curChannelVo.getChannelLink())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }  
	    if (StringUtil.isEmpty(curChannelVo.getChannelSort())) {
	    	curChannelVo.setChannelSort("0");
		  }  
		    
	    if (StringUtil.isEmpty(curChannelVo.getIsAvailable())) {
	    	curChannelVo.setIsAvailable("1");
		  }   
	    
	    logger.info("insertChannel: input data check end");	    
	    return BaseResult.success(channelDao.insertChannel(curChannelVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
	   // TODO Auto-generated method stub
	   logger.info("deleteById begin");
	   logger.info("deleteById Id:" +Id);
					  
	   return BaseResult.success(channelDao.deleteById(Id));
	}

	@Override
	public BaseResult deleteChannelBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteChannelBatch begin");
	    logger.info("deleteChannelBatch Ids:" +id.toString());
	    logger.info("deleteChannelBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteChannelBatch: input data check end");
	    
	    return BaseResult.success(channelDao.deleteChannelBatch(id));
	}

	@Override
	public BaseResult updateById(ChannelVo curChannelVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curChannelVo:" +JSON.toJSONString(curChannelVo));
	  
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curChannelVo.getChannelName())) {
	      baseResult =BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curChannelVo.getChannelLink())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    if (StringUtil.isEmpty(curChannelVo.getChannelSort())) {
	    	curChannelVo.setChannelSort("0");
		}  
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(channelDao.updateById(curChannelVo));
	}

	@Override
	public BaseResult selectByChannelForPage(ChannelVo curChannelVo,PaginationInfo paginationInfo) {
		// TODO Auto-generated method stub
	    logger.info("selectByHelpCenterInfoForPage begin.");
	    if (!StringUtil.isEmpty(curChannelVo.getChannelName())) {
	    	curChannelVo.setChannelName(curChannelVo.getChannelName().replace(" ", ""));
	    }
	    logger.info("selectByHelpCenterInfoForPage Title:" +curChannelVo.getChannelName());
	
	    logger.info("selectByHelpCenterInfoForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curChannelVo.setPaginationInfo(paginationInfo);
	    List<ChannelVo> list=channelDao.selectByChannelForPage(curChannelVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByHelpCenterInfoForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult selectById(ChannelVo curChannelVo) {
		// TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curChannelVo.getId());
	  
	    return BaseResult.success(channelDao.selectById(curChannelVo.getId()));
	}

	@Override
	public Boolean checkChannelName(String channelName) {
		// TODO Auto-generated method stub
	    logger.info("checkChannelName begin");
	    logger.info("checkChannelName channelName:" +channelName);
	    logger.info("checkChannelName: input data check begin");   
	  
	    if (StringUtil.isEmpty(channelName)) {       
	      return false;
	    }
	    logger.info("checkChannelName: input data check end");
	    
	    ChannelVo channelVo=new ChannelVo();	   
	    channelVo.setChannelName(channelName);
	    int count=channelDao.count(channelVo);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

	@Override
	public BaseResult selectChannel() {
		// TODO Auto-generated method stub
		 logger.info("selectChannel begin");	 
						  
		 return BaseResult.success(channelDao.selectChannel());
	}

}
